#include <windows.h>
#include "stdafx.h"
#include "YuvToRGB.h"

#define MYUCHAR(_flt_) ((_flt_>0 && _flt_<256)?_flt_:((_flt_<0)?0:255))

// U0 Y0 Vo Y1 | U2 Y2 V2 Y3 | U4 Y4 V4 Y5 | ...
void YUV422UYVY_ToRGB24(unsigned char* pbYUV, unsigned char* pbRGB, DWORD yuv_len)
{

//	 /*
	int B_Cb128,R_Cr128,G_CrCb128;
    int Y0,U,Y1,V;
    int R,G,B;

	BYTE *yuv_index;
	BYTE *rgb_index;

//	long lStride_out = (((dwWidth * 3) + 3) / 4) * 4;

	yuv_index = pbYUV;
	rgb_index = pbRGB;

    for (DWORD i = 0, j = 0; i < yuv_len; )
    {
        // Each WORD is a 'UY' or 'VY' block. 
        // Set the low byte (chroma) to 0x80 and leave the high byte (luma)
//            WORD pixel = pwSource[x] & 0xFF00;
//          pixel |= 0x0080;
//        pwTarget[x] = pixel;

// R = 1.164(Y - 16)					+ 1.596(V - 128)
// G = 1.164(Y - 16)					- 0.813(V - 128) - 0.391(U - 128)
// B = 1.164(Y - 16)                   + 2.018(U - 128)

		U  = (int)((float)yuv_index[i++]-128.0f);
		Y0 = (int)(1.164f * ((float)pbYUV[i++]-16.0f));
		V  = (int)((float)yuv_index[i++]-128.0f);
		Y1 = (int)(1.164f * ((float)yuv_index[i++]-16.0f));   

		R_Cr128   =  (int)(1.596f*V);
		G_CrCb128 = (int)(-0.813f*V - 0.391f*U);
		B_Cb128   =  (int)(2.018f*U);

		R= Y0 + R_Cr128;
		G = Y0 + G_CrCb128;
		B = Y0 + B_Cb128;

		rgb_index[j++] = max(0, min(B, 255));
		rgb_index[j++] = max(0, min(G, 255));
		rgb_index[j++] = max(0, min(R, 255));

		R= Y1 + R_Cr128;
		G = Y1 + G_CrCb128;
		B = Y1 + B_Cb128;

		rgb_index[j++] = max(0, min(B, 255));
		rgb_index[j++] = max(0, min(G, 255));
		rgb_index[j++] = max(0, min(R, 255));
	}
//	*/
}
